﻿#region Using Statements
using System;
using Microsoft.Xna.Framework;
#endregion

namespace StarFox.ScreenComponents
{
    /// <summary>
    /// Implementation of a head-up display.
    /// <para>Definition of lives, score, position, time and speed items.</para>
    /// </summary>
    partial class HeadUpDisplay
    {
        #region Lives

        Vector2 initPosLives;
        Vector2 endPosLives;
        Vector2 posLives;
        float distToLives;
        string livesText = "00";

        /// <summary>
        /// Sets the current number of lives available.
        /// </summary>
        public int Lives
        {
            set
            {
                if (value < 10)
                    livesText = "0" + value.ToString();
                else
                    livesText = value.ToString();
            }
        }

        #endregion

        #region Score

        Vector2 initPosScore;
        Vector2 endPosScore;
        Vector2 posScore;
        float distToScore;
        float distToTime;
        float distToPosition;
        float distToSpeed;

        // backing fields
        int score;
        string scoreText = "000000";

        /// <summary>
        /// Sets the score currently achieved.
        /// </summary>
        public int Score
        {
            set
            {
                score = value;
                int scoreLength = score.ToString().Length;
                scoreText = string.Empty;

                for (int i = 0; i < (6 - scoreLength); i++) { scoreText += "0"; }

                scoreText += score.ToString();
            }
        }

        #endregion

        #region Position

        // backing fields
        float position;
        string positionText;

        /// <summary>
        /// Sets the level distance currently covered.
        /// </summary>
        public float LevelPosition
        {
            set
            {
                position = MathHelper.Clamp(value, 0.0f, 1.0f);
                double auxValue = Math.Round(position * 100, 0);
                positionText = auxValue.ToString() + "%";
            }
        }

        #endregion

        #region Time

        // backing fields
        TimeSpan time;
        string timeText;

        /// <summary>
        /// Sets the current elapsed game time.
        /// </summary>
        public TimeSpan PlayTime
        {
            set
            {
                time = value;

                string strCentsOfSecond;
                string strSeconds;
                string strMinutes;

                int centsOfSecond = time.Milliseconds / 10;

                if (centsOfSecond < 10)
                    strCentsOfSecond = "0" + centsOfSecond.ToString();
                else
                    strCentsOfSecond = centsOfSecond.ToString();

                if (time.Seconds < 10)
                    strSeconds = "0" + time.Seconds.ToString();
                else
                    strSeconds = time.Seconds.ToString();

                if (time.Minutes < 10)
                    strMinutes = "0" + time.Minutes.ToString();
                else
                    strMinutes = time.Minutes.ToString();

                timeText = strMinutes + "' " + strSeconds + "'' " + strCentsOfSecond;
            }
        }

        #endregion

        #region Speed

        // backing fields
        float speed;
        string speedText;

        /// <summary>
        /// Sets the current speed.
        /// </summary>
        public float Speed
        {
            set
            {
                speed = MathHelper.Clamp(value, 0.0f, 1.0f);
                double auxValue = Math.Round(speed * 100, 0);
                speedText = auxValue.ToString() + "%";
            }
        }

        #endregion

        private void HeadUpDisplay_Score()
        {
            Vector2 auxStrSize;

            auxStrSize = smallFont.MeasureString("LIVES") * Screen.Scale;
            initPosLives = new Vector2(Screen.Width * 0.040f, -auxStrSize.Y);
            endPosLives = new Vector2(initPosLives.X, Screen.Height * 0.025f);
            posLives = initPosLives;
            distToLives = auxStrSize.X + Screen.Width * 0.02f;

            auxStrSize = smallFont.MeasureString("SCORE") * Screen.Scale;
            initPosScore = new Vector2(Screen.Width * 0.65f, -(auxStrSize.Y * 2 + Screen.Height * 0.05f));
            endPosScore = new Vector2(initPosScore.X, Screen.Height * 0.0275f);
            posScore = initPosScore;
            distToScore = Screen.Width * 0.1215f;
            distToPosition = auxStrSize.Y + Screen.Height * 0.0075f;
            distToTime = distToPosition * 2;
            distToSpeed = distToPosition * 3.5f;
        }
    }
}
